package org.brisling.service.sysman;

import java.util.List;

import org.brisling.domain.common.ListDomain;
import org.brisling.domain.sysman.Users;
import org.brisling.domain.sysman.inf.BizBeanDataModeler;
import org.brisling.repository.inf.ListByUpLinkRepository;
import org.brisling.repository.sysman.inf.UsersRepo;
import org.brisling.service.common.BaseUplinkService;
import org.brisling.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/sysmanList")
public class SysmanListService extends BaseUplinkService{
	
	@SuppressWarnings("rawtypes")
	private ListByUpLinkRepository _repo= null;
	
	
	private BizBeanDataModeler _domain = null;
	
	
	
	@Autowired
	private UsersRepo userrepo;
	
	
	
	
	@RequestMapping("/getListByUplinkid/{resourcename}")
	@ResponseBody
	public ListDomain<? extends BizBeanDataModeler> getListByDeptid(@RequestParam(value="uplinkid",required=false) String tid,
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort,
			@PathVariable("resourcename") String resourceName,
			@RequestBody String body){
		
		ListDomain<? extends BizBeanDataModeler> listDomain =null;
		if(checkPathVariable(resourceName))
			listDomain = super.getPagedListByUplinkid(tid, page, rows, _repo);
				
		return listDomain;
		
		
	}
	

	@RequestMapping("/getList/{resourcename}")
	@ResponseBody
	public List<? extends BizBeanDataModeler> getList(@RequestParam(value="uplinkid",required=false) String tid,
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort,
			@PathVariable("resourcename") String resourceName,
			@RequestBody String body){
		
		List<? extends BizBeanDataModeler> listDomain =null;
		if(checkPathVariable(resourceName))
			listDomain = super.getList(tid, _repo);		
		
		return listDomain;		
	}
	
	
	@RequestMapping("/getOne/{resourcename}")
	@ResponseBody
	public <T extends BizBeanDataModeler>T getOne(@RequestParam(value="tid",required=false) String tid,
			
			@PathVariable("resourcename") String resourceName
			){
		
		T t =null;
		if(checkPathVariable(resourceName))
				t= super.getOne(tid, _repo);
		
		return t;
	}
	
	
	@RequestMapping("/getAll/{resourcename}")
	@ResponseBody
	public List<? extends BizBeanDataModeler> getAll(@PathVariable("resourcename") String resourceName
	){
		
		List<? extends BizBeanDataModeler> listDomain =null;
		if(checkPathVariable(resourceName))
			listDomain = super.getAll(_repo);		
		
		return listDomain;		
	}
	
	@SuppressWarnings({ "unchecked" })
	@RequestMapping("/save/{resourcename}")
	@ResponseBody
	public <T extends BizBeanDataModeler> T save(
			@RequestParam(value="id",required=false) String tid,
			@PathVariable("resourcename") String resourceName,
			@RequestBody String body){
		
		T obj = null;
		if(checkPathVariable(resourceName)){
			obj = (T) JsonUtil.bodyToObj(body, _domain.getClass());		
			return  super.save(_repo, obj);
		}
		return null;
	}
	
	@RequestMapping("/del/{resourcename}")
	@ResponseBody
	public String del(@RequestParam(value="id",required=false) String tid,
			@PathVariable("resourcename") String resourceName,
			@RequestBody String body){
			
		if(checkPathVariable(resourceName))		
			return super.del(_repo,tid);
		
		return null;
	}
	
	private Boolean checkPathVariable(String _resourcename){
		Boolean cased = false;
		if(_resourcename!=null){
			switch(_resourcename){
				case "user":
					_repo = userrepo;
					_domain = new Users();
					cased = true;
					break;						
			}
		}
		return cased;
	}
}
